package com.adv.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.adv.adapter.CatalogAdapter;
import com.adv.object.CatalogObj;

public class Catalog extends SherlockActivity {
	public static String CATALOG_ID_KEY = "catalog_id";
	private boolean preExit = false;
	private ActionBar actionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_catalog);
		
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setSubtitle("Lĩnh vực khuyến mãi");

		ListView lCatalog = (ListView) findViewById(R.id.list_catalog);

		//TODO: set value from json
		final List<CatalogObj> objects = new ArrayList<CatalogObj>();
		objects.add(new CatalogObj(
				1212,
				"Heell",
				"http://p4.img.cctvpic.com/nettv/english/program/documentary/20110816/images/104307_1313464978102.jpg"));
		objects.add(new CatalogObj(
				453,
				"Heell",
				"http://p4.img.cctvpic.com/nettv/english/program/documentary/20110816/images/104307_1313464978102.jpg"));

		lCatalog.setAdapter(new CatalogAdapter(this, R.layout.base_item,
				objects));
		lCatalog.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos,
					long id) {
				Intent i = new Intent(Catalog.this, Distributors.class);
				i.putExtra(CATALOG_ID_KEY, objects.get(pos).getId());
				startActivity(i);
				Log.e(getPackageName(), "Catalog ->Send ID:"
						+ objects.get(pos).getId());
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		preExit = false;
	}

	@Override
	public void onBackPressed() {

		if (preExit) {
			super.onBackPressed();
			return;
		}
		preExit = true;
		Toast.makeText(Catalog.this, "Ấn lại lần nữa để thoát",
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu, menu);
		menu.removeItem(R.id.action_home);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId,
			com.actionbarsherlock.view.MenuItem item) {
		if(item.getItemId()==android.R.id.home){
			onBackPressed();
		}
		return true;
	}
}
